const Goods = require('../model/goods.model')
class GoodsService {
    async createGoods(goods) {
        // console.log('发布成功');
        // return {
        //     goods_name: '蓝牙音响'

        // }

        const res = await Goods.create(goods);
        return res.dataValues
    }
    async updtaeGoods(id, goods) {
        const res = await Goods.update(goods, { where: { id } })
        return res[0] > 0 ? true : false
    }
    async removeGoods(id) {
        const res = await Goods.destroy({ where: { id } }); // 删除数据库
        return res > 0 ? true : false
    }
    async restoreGoods(id) {
        const res = await Goods.restore({ where: { id } }); // 删除数据库
        return res > 0 ? true : false
    }

    async findGoods(page, pageSize) {
        // 获取总数
        // 获取分页数
        // const count = await Goods.count();
        // const offset = (page - 1) * pageSize;
        // const rows = await Goods.findAll({ offset: offset, limit: pageSize * 1 })
        const offset = (page - 1) * pageSize;
        const { count, rows } = await Goods.findAndCountAll({ offset: offset, limit: pageSize * 1 })
        return {
            page,
            pageSize,
            total: count - 1,
            list: rows
        }
    }
}

module.exports = new GoodsService()